function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Checkbox } from 'antd';
import React, { useContext, useLayoutEffect, useState } from 'react';
import { areEqual, CellTypeEnum, DsContext, TableSelectionModel, UpdateFlagEnum } from "../base/common";
import { stopPropagation, useIUPEffect, useRefCallback, util } from "../util";
import { jsx as _jsx } from "react/jsx-runtime";
var updateState = [UpdateFlagEnum.ForceUpdate, UpdateFlagEnum.ColumnChecked];
function isDisabled(_ref) {
  var checkDisabled = _ref.checkDisabled,
    row = _ref.row,
    dataIndex = _ref.dataIndex,
    table = _ref.table;
  if ([CellTypeEnum.AggregateCell, CellTypeEnum.ExpandRowCell].includes(row.cellType)) {
    return true;
  }
  return util.isFunction(checkDisabled) ? checkDisabled({
    row: row,
    dataIndex: dataIndex
  }) : !!table.isRowSelectionDisabled(row);
}
export function loopChildren(table, row, dataIndex, checked, rowIndex) {
  var _table$props$rowSelec;
  var loop = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
  var updateRows = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : [];
  var checkDisabled = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : undefined;
  if (!row) {
    return false;
  }
  var children = row.children;
  var defaultChecked = !!row[dataIndex];
  if (defaultChecked !== checked) {
    if (!isDisabled({
      checkDisabled: checkDisabled,
      row: row,
      dataIndex: dataIndex,
      table: table
    })) {
      row[dataIndex] = checked;
      row.__update__ = {
        rowIndex: rowIndex,
        dataIndex: dataIndex
      };
      updateRows.push(row);
    }
  }
  var needLoop = loop || ((_table$props$rowSelec = table.props.rowSelection) === null || _table$props$rowSelec === void 0 ? void 0 : _table$props$rowSelec.autoCheckedChildren) !== false;
  if (needLoop && children) {
    for (var i = 0, len = children.length; i < len; i++) {
      loopChildren(table, children[i], dataIndex, checked, rowIndex, needLoop, updateRows, checkDisabled);
    }
  }
  return !!row.__update__;
}

/**
 * 表头复选框
 */
export var AllCheckBox = /*#__PURE__*/React.memo(function (_ref2) {
  var table = _ref2.table,
    checkedDataIndex = _ref2.dataIndex,
    checkDisabled = _ref2.checkDisabled,
    children = _ref2.children;
  var dataSource = useContext(DsContext);
  var dataIndex = checkedDataIndex || table.getCheckBoxDataIndex();
  var _useState = useState({
      checked: false,
      indeterminate: false
    }),
    _useState2 = _slicedToArray(_useState, 2),
    state = _useState2[0],
    setState = _useState2[1];
  var onCheckedHandler = useRefCallback(function (e) {
    var checked = e.target.checked;
    var checkedData = [];
    var updateRows = [];
    setState({
      checked: checked,
      indeterminate: false
    });
    for (var i = 0, len = dataSource.length; i < len; i++) {
      var row = dataSource[i];
      if (loopChildren(table, row, dataIndex, checked, i, false, [], checkDisabled)) {
        updateRows.push(row);
      }
      if (row[dataIndex]) {
        checkedData.push(row);
      }
    }
    table.onCheckedChange(checkedData, updateRows, checked);
  });
  useIUPEffect(function () {
    setState(function () {
      var checkedSize = 0,
        disabledSize = 0;
      var dataSize = dataSource.length;
      dataSource.forEach(function (r) {
        if (r[dataIndex]) {
          checkedSize++;
        } else if (isDisabled({
          checkDisabled: checkDisabled,
          row: r,
          dataIndex: dataIndex,
          table: table
        })) {
          disabledSize++;
        }
      });
      var calcSize = checkedSize + disabledSize;
      return {
        checked: dataSize === calcSize && dataSize > 0,
        indeterminate: checkedSize > 0 && calcSize < dataSize
      };
    });
  }, [dataSource]);
  return /*#__PURE__*/_jsx(Checkbox, _objectSpread(_objectSpread({}, state), {}, {
    onClick: stopPropagation,
    style: {
      margin: 'auto'
    },
    onChange: onCheckedHandler,
    children: children
  }));
}, function (p, n) {
  if (updateState.includes(n.table.updateFlag)) {
    return false;
  }
  return areEqual(p, n);
});

/**
 * 表体复选框
 * @param row
 * @param dataIndex
 * @param rowIndex
 * @param checkDisabled 自定义disabled权限
 * @constructor
 */
export var RowCheckBox = /*#__PURE__*/React.memo(function (_ref3) {
  var _table$props$rowSelec2;
  var table = _ref3.table,
    row = _ref3.row,
    dataIndex = _ref3.dataIndex,
    rowIndex = _ref3.rowIndex,
    checkDisabled = _ref3.checkDisabled;
  var _useState3 = useState(row[dataIndex]),
    _useState4 = _slicedToArray(_useState3, 2),
    checked = _useState4[0],
    setChecked = _useState4[1];
  var checkedHandler = useRefCallback(function (checked) {
    if (checked === row[dataIndex]) {
      return;
    }
    var dataSource = table.state.dataSource;
    loopChildren(table, row, dataIndex, checked, rowIndex, false, [], checkDisabled);
    var checkedData = dataSource.filter(function (r) {
      return r[dataIndex] === true;
    });
    table.onCheckedChange(checkedData, row, checked);
  });
  useIUPEffect(function () {
    if (checked !== row[dataIndex]) {
      setChecked(row[dataIndex]);
    }
  }, [row, row[dataIndex]]);
  useLayoutEffect(function () {
    checkedHandler(checked);
  }, [checked]);
  var disabled = isDisabled({
    checkDisabled: checkDisabled,
    row: row,
    dataIndex: dataIndex,
    table: table
  });
  var rowSelectionType = (_table$props$rowSelec2 = table.props.rowSelection) === null || _table$props$rowSelec2 === void 0 ? void 0 : _table$props$rowSelec2.type;
  if (disabled) {
    return checkDisabled ? /*#__PURE__*/_jsx("div", {
      className: "cover-cell",
      children: /*#__PURE__*/_jsx(Checkbox, {
        disabled: true,
        className: "cover-cell",
        checked: checked
      })
    }) : null;
  }
  var clickHandler = function clickHandler(e) {
    stopPropagation(e);
    if (table.props.rowSelected && table.props.checkboxSelected && rowSelectionType !== TableSelectionModel.MULTIPLE_INTERVAL) {
      table.setHighlight(rowIndex, {
        ctrlKey: false,
        shiftKey: false
      });
    }
  };
  var onCheckClick = function onCheckClick(e) {
    setChecked(e.target.checked);
  };
  return /*#__PURE__*/_jsx("div", {
    className: "cover-cell",
    onClick: clickHandler,
    children: /*#__PURE__*/_jsx(Checkbox, {
      className: "cover-cell",
      onChange: onCheckClick,
      checked: checked
    })
  });
}, function (p, n) {
  if (n.row.__update__ || updateState.includes(n.table.updateFlag)) {
    return false;
  }
  return areEqual(p, n);
});